package com.shux.springsource.beanpost;

import java.util.HashSet;
import java.util.Set;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionVisitor;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.util.StringValueResolver;

/**
 * 描述：通过beanFactoryPostProcessor过滤掉指定的值，注意不是过滤指定的属性
 * 
 * <pre>HISTORY
 * ****************************************************************************
 *  ID   DATE           PERSON          REASON
 *  1    2018年2月2日      Simba.Hua         Create
 * ****************************************************************************
 * </pre>
 * @author Simba.Hua
 */
public class SensitivePropertyRemovingBeanFactoryPostProcessor implements BeanFactoryPostProcessor{
	private Set<String> sensitiveMsg;
	
	public SensitivePropertyRemovingBeanFactoryPostProcessor() {
		sensitiveMsg = new HashSet<>();
	}
	@Override
	public void postProcessBeanFactory(
			ConfigurableListableBeanFactory beanFactory) throws BeansException {
		String [] beanNames = beanFactory.getBeanDefinitionNames();
		for (String beanName : beanNames) {
			BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
			BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(strVal -> {
				if (sensitiveMsg.contains(strVal.toUpperCase())) {
					return "******";
				}
				return strVal;
			});
			visitor.visitBeanDefinition(beanDefinition);
		}
	}
	
	public void setSensitiveMsg(Set<String> sensitiveMsg) {
		//this.sensitiveMsg = sensitiveMsg;
		this.sensitiveMsg.clear();
		for (String msg : sensitiveMsg) {
			this.sensitiveMsg.add(msg.toUpperCase());
		}
		
	}
	public Set<String> getSensitiveMsg() {
		return sensitiveMsg;
	}
}
